package com.example.poem.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dueeeke.videocontroller.component.PrepareView;
import com.example.poem.R;
import com.example.poem.api.Api;
import com.example.poem.api.ApiConfig;
import com.example.poem.api.NetworkCallback;
import com.example.poem.entity.VideoEntity;
import com.example.poem.listener.OnItemChildClickListener;
import com.example.poem.listener.OnItemClickListener;
import com.example.poem.view.CircleTransform;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    List<VideoEntity> datas;
    private OnItemChildClickListener mOnItemChildClickListener;
    private OnItemClickListener mOnItemClickListener;

    public void setDatas(List<VideoEntity> datas) {
        this.datas = datas;
    }

    public VideoAdapter(Context context, List<VideoEntity> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    public VideoAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_video_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        VideoEntity videoEntity = datas.get(position);
        VideoEntity.VideoSocialEntity videoSocialEntity = videoEntity.getVideoSocialEntity();
        if(videoSocialEntity!=null){
            int likenum = videoEntity.getVideoSocialEntity().getLikenum();
            int commentnum = videoEntity.getVideoSocialEntity().getCommentnum();
            int collectnum = videoEntity.getVideoSocialEntity().getCollectnum();
            boolean flagLike = videoEntity.getVideoSocialEntity().isFlagLike();
            boolean flagCollect = videoEntity.getVideoSocialEntity().isFlagCollect();
            Log.d("flagCollect",String.valueOf(flagCollect));
            Log.d("flagLike",String.valueOf(flagLike));
            if (flagLike) {
                viewHolder.tvLike.setTextColor(Color.parseColor("#E21918"));
                viewHolder.imgLike.setImageResource(R.mipmap.like_select);
            }
            if (flagCollect) {
                viewHolder.tvCollect.setTextColor(Color.parseColor("#E21918"));
                viewHolder.imgCollect.setImageResource(R.mipmap.collect_select);
            }
            viewHolder.tvLike.setText(String.valueOf(likenum));
            viewHolder.tvComment.setText(String.valueOf(commentnum));
            viewHolder.tvCollect.setText(String.valueOf(collectnum));
            viewHolder.flagCollect = flagCollect;
            viewHolder.flagLike = flagLike;
        }
        Picasso.with(mContext).load(videoEntity.getHeadurl()).transform(new CircleTransform()).into(viewHolder.imageHeader);
        Picasso.with(mContext).load(videoEntity.getCoverurl()).into(viewHolder.mThumb);

        viewHolder.mPosition = position;
    }

    @Override
    public int getItemCount() {
        if (datas != null && datas.size() > 0) {
            return datas.size();
        } else {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private static final int COLLECT = 1;
        private static final int LIKE = 2;
        private TextView tvTitle;
        private TextView tvAuthor;
        private TextView tvLike;
        private TextView tvComment;
        private TextView tvCollect;
        private ImageView imageHeader;
        private ImageView imgComment;
        private ImageView imgCollect;
        private ImageView imgLike;
        private ImageView mThumb;
        public PrepareView mPrepareView;
        public FrameLayout mPlayerContainer;
        private boolean flagCollect;
        private boolean flagLike;
        public int mPosition;

        public ViewHolder(@NonNull View view) {
            super(view);
            tvTitle = view.findViewById(R.id.title);
            tvAuthor = view.findViewById(R.id.author);
            tvLike = view.findViewById(R.id.like);
            tvComment = view.findViewById(R.id.comment);
            tvCollect = view.findViewById(R.id.collect);
            imageHeader = view.findViewById(R.id.img_header);
            imgComment = view.findViewById(R.id.img_comment);
            imgCollect = view.findViewById(R.id.img_collect);
            imgLike = view.findViewById(R.id.img_like);
            mPlayerContainer = view.findViewById(R.id.player_container);
            mPrepareView = view.findViewById(R.id.prepare_view);
            mThumb = mPrepareView.findViewById(R.id.thumb);
            if (mOnItemChildClickListener != null) {
                mPlayerContainer.setOnClickListener(this);
            }
            if (mOnItemClickListener != null) {
                view.setOnClickListener(this);
            }


            imgCollect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int collectNum = Integer.parseInt(tvCollect.getText().toString());
                    if(flagCollect){
                        if(collectNum>0){
                            tvCollect.setText(String.valueOf(--collectNum));
                            tvCollect.setTextColor(Color.parseColor("#161616"));
                            imgCollect.setImageResource(R.mipmap.collect);
                            update(COLLECT,datas.get(mPosition).getVid(),!flagCollect);
                        }
                    }else{
                        tvCollect.setText(String.valueOf(++collectNum));
                        tvCollect.setTextColor(Color.parseColor("#E21918"));
                        imgCollect.setImageResource(R.mipmap.collect_select);
                        update(COLLECT,datas.get(mPosition).getVid(),!flagCollect);
                    }
                    flagCollect = !flagCollect;
                }
            });

            imgLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int likeNum = Integer.parseInt(tvLike.getText().toString());
                    if(flagLike){
                        if(likeNum>0){
                            tvLike.setText(String.valueOf(--likeNum));
                            tvLike.setTextColor(Color.parseColor("#161616"));
                            imgLike.setImageResource(R.mipmap.like);
                            update(LIKE,datas.get(mPosition).getVid(),!flagLike);
                        }
                    }else{
                        tvLike.setText(String.valueOf(++likeNum));
                        tvLike.setTextColor(Color.parseColor("#E21918"));
                        imgLike.setImageResource(R.mipmap.like_select);
                        update(LIKE,datas.get(mPosition).getVid(),!flagLike);
                    }
                    flagLike = !flagLike;
                }
            });
            view.setTag(this);
        }



        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.player_container) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChildClick(mPosition);
                }
            } else {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(mPosition);
                }
            }
        }
    }

    private void update(int type,int vid,boolean flag) {
        HashMap<String,Object> params = new HashMap<>();
        params.put("type",type);
        params.put("vid",vid);
        params.put("flag",flag);
        Api.config(ApiConfig.VIDEO_UPDATE_COUNT,params).postRequest(mContext,new NetworkCallback() {
            @Override
            public void onSuccess(String result) {

            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        mOnItemChildClickListener = onItemChildClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
